package com.cqemme.comm.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.*;


/**
 *
 * ClassName:DateUtils
 * @Description:
 * TODO
 * Company:EM ChongQing SCIENCE And TECHNOLOGY CO.LTD
 * date:2019-06-05
 * @Author:Tommy
 *
 *
 * ******************************************************
 * HISTORY
 * 2019-06-05 add by Tommy
 *
 *
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {


		public static final String regex1="^((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)$";
		public static final String regex2="^((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)\\s(([0-1]{1}\\d{1})|([2]{1}[0-3]))\\:([0-5]{1}\\d{1})\\:([0-5]{1}\\d{1})$";
		public static final String regex3="^((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)\\s(([0-1]{1}\\d{1})|([2]{1}[0-3]))\\:([0-5]{1}\\d{1})$";
		public static final String regex4="^((?!0000)[0-9]{4}/((0[1-9]|1[0-2])/(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])/(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)$";
		public static final String regex5="^((?!0000)[0-9]{4}/((0[1-9]|1[0-2])/(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])/(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)\\s(([0-1]{1}\\d{1})|([2]{1}[0-3]))\\:([0-5]{1}\\d{1})\\:([0-5]{1}\\d{1})$";
		public static final String regex6="^((?!0000)[0-9]{4}/((0[1-9]|1[0-2])/(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])/(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)\\s(([0-1]{1}\\d{1})|([2]{1}[0-3]))\\:([0-5]{1}\\d{1})$";
		public static final String regex7="^((?!0000)[0-9]{4}((0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])(29|30)|(0[13578]|1[02])31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)0229)$";
		public static final String regex8="^((?!0000)[0-9]{4}((0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])(29|30)|(0[13578]|1[02])31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)0229)(([0-1]{1}\\d{1})|([2]{1}[0-3]))([0-5]{1}\\d{1})([0-5]{1}\\d{1})$";
		public static final String regex9="^((?!0000)[0-9]{4}((0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])(29|30)|(0[13578]|1[02])31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)0229)(([0-1]{1}\\d{1})|([2]{1}[0-3]))([0-5]{1}\\d{1})$";
		public static final String regex10="^\\d{13}$";
		public static final String regex11="^(([0-1]{1}\\d{1})|([2]{1}[0-3]))\\:([0-5]{1}\\d{1})$";
		public static final String regex12="^(([0-1]{1}\\d{1})|([2]{1}[0-3]))\\:([0-5]{1}\\d{1})\\:([0-5]{1}\\d{1})$";


		/**
		 * yyyy-MM-dd HH:mm:ss
		 */
		public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";


		public static final String NOT_YMD_HH_MM_SS = "HH:mm:ss";
		/**
		 * yyyy-MM-dd_HH:mm:ss
		 */
		public static final String YYYY_MM_DD_HH_MM_SS_T = "yyyy-MM-dd_HH:mm:ss";
		/**
		 * yyyy-MM-ddTHH:mm:ss
		 */
		public static final String YYYY_MM_DD_HH_MM_SS_TT = "yyyy-MM-ddTHH:mm:ss";
		/**
		 * yyyy-MM-dd hh:mm:ss
		 */
		public static final String YYYY_MM_DD_HH_MM_SS_12 = "yyyy-MM-dd hh:mm:ss";
		/**
		 * yyyy-MM-dd HH:mm:ss.S
		 */
		public static final String YYYY_MM_DD_HH_MM_SS_s = "yyyy-MM-dd HH:mm:ss.S";
		/**
		 * yyyy.MM.dd HH:mm:ss
		 */
		public static final String YYYY_MM_DD_HH_MM_SS_POINT = "yyyy.MM.dd HH:mm:ss";

		/**
		 * yyyy-MM-dd
		 */
		public static final String YYYY_MM_DD_LINE = "yyyy-MM-dd";
		/** yyyy-MM */
		public static final String yyyy_mm = "yyyy-MM";
		/**
		 * yyyy.MM.dd
		 */
		public static final String YYYY_MM_DD_POINT = "yyyy.MM.dd";

		/**
		 * yyyy.MM.dd HH:mm
		 */
		public static final String YYYY_MM_DD_HH_MM_POINT = "yyyy.MM.dd HH:mm";

		/**
		 * yyyy-MM-dd HH:mm
		 */
		public static final String YYYY_MM_DD_HH_MM_LINE = "yyyy-MM-dd HH:mm";
		/**
		 * HH:mm am
		 */
		public static final String HH_MM_A_Z = "HH:mm a";
		/**
		 * MM-dd HH:mm
		 */
		public static final String MM_DD_HH_MM = "MM-dd HH:mm";
	    /**
	    * MM-dd HH:mm:ss
	    */
	    public static final String MM_DD_HH_MM_SS = "MM-dd HH:mm:ss";
		/**
		 * MM/dd/yyyy
		 */
		public static final String MM_DD_YYYY = "MM/dd/yyyy";

		public static final String MMddyyyyHms="MM/dd/yyyy HH:mm:ss";
		/**
		 * 20140506
		 */
		public static final String YYYY_MM_DD = "yyyyMMdd";
		/**
		 * 201405
		 */
		public static final String YYYY_MM = "yyyyMM";
		/**
		 * yyyy-MM
		 */
		public static final String yyyy_MM_STR = "yyyy-MM";
		/**
		 * yyyy:MM:dd:HH:mm:ss
		 */
		public static final String yyyy_MM_DD_HH_MM_SS="yyyy:MM:dd:HH:mm:ss";
		/**
		 * 02.18.2015 12:00:00
		 */
		public static final String MM_DD_YYYY_HH_MM_SS = "MM.dd.yyyy HH:mm:ss";
		/**
		 * 2015年03月13日
		 */
		public static final String YYYY_MM_DD_CH = "yyyy年MM月dd日";
		/**
		 * 2015年03月13日09:30
		 */
		public static final String YYYY_MM_DD_HH_MM_CH = "yyyy年MM月dd日HH:mm";
		/**
		 * 12:00
		 */
		public static final String HH_MM = "HH:mm";
		/**
		 * yyyyMMddHHmmSSS
		 */
		public static final String YYYYMMDDHHMMSSS = "yyyyMMddHHmmssS";

		public static final String YYYYMMDDHHmmss = "yyyyMMddHHmmss";
		/**
		 * yyyyMMddHHmm
		 */
		public static final String YYYYMMDDHHMM = "yyyyMMddHHmm";

		public static final String yyyyMMdd_HHmmss ="yyyyMMdd_HHmmss";
		/**
		 * yyyyMMddHHmmSS
		 */
		public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmSS";
		/**yyyyMMdd HH:mm:ss*/
		public static final String YYYYMMDD_HH_MM_SS = "yyyyMMdd HH:mm:ss";
		/**yyyy年MM月dd日 HH:mm:ss**/
		public static final String YYYY_MM_DD_HHMMSS="yyyy年MM月dd日 HH:mm:ss";

		private Log log = LogFactory.getLog(DateUtils.class);
		public static final String startStr="start";
		public static final String endStr="end";
		   /**
		    * 判断是否是指定格式的时间字符串
		    * @Description: TODO
		    * @param: @param dateString
		    * @param: @return
		    * @return: boolean
		    * throws:
		    *
		    */
	       public static boolean checkDateStr(String dateString) {
	   		List<String> regexs=Arrays.asList(regex1,regex2,regex3,regex4,regex5,regex6,regex7,regex8,regex9,regex10,regex11,regex12);
	   		for (String regex : regexs) {
	   			if(dateString.matches(regex)) {
	   				return true;
	   			}
	   		}
	   		return false;
	   	   }
	       /**
	        * 获取该字符串正则
	        * @Description: TODO
	        * @param: @param dateString
	        * @param: @return
	        * @return: String
	        * throws:
	        *
	        */
	       public static String getRegex(String dateString) {
		   		List<String> regexs=Arrays.asList(regex1,regex2,regex3,regex4,regex5,regex6,regex7,regex8,regex9,regex10,regex11,regex12);
		   		for (String regex : regexs) {
		   			if(dateString.matches(regex)) {
		   				return regex;
		   			}
		   		}
		   		return null;
		  }
	       /**
	        * 正则存放 map
	        */
	       public static Map<String, Integer> REGEXS = new HashMap<String, Integer>();
	   		static {
	   			REGEXS.put(regex1, 1);
	   			REGEXS.put(regex2, 2);
	   			REGEXS.put(regex3, 3);
	   			REGEXS.put(regex4, 4);
	   			REGEXS.put(regex5, 5);
	   			REGEXS.put(regex6, 6);
	   			REGEXS.put(regex7, 7);
	   			REGEXS.put(regex8, 8);
	   			REGEXS.put(regex9, 9);
	   			REGEXS.put(regex10, 10);
				REGEXS.put(regex11, 11);
				REGEXS.put(regex12, 12);

	   	 }
	   	  /**
	   	   * 格式化 字符存放map
	   	   */
	   	 public static Map<Integer,String> FORMATSTR = new HashMap<Integer,String>();
			static {
				/****
		   		 * yyyy-MM-dd
		   		 * yyyy-MM-dd HH:mm:ss
		   		 * yyyy-MM-dd HH:mm
		   		 * yyyy/MM/dd
		   		 * yyyy/MM/dd HH:mm:ss
		   		 * yyyy/MM/dd HH:mm
		   		 * yyyyMMdd
		   		 * yyyyMMddHHmmss
		   		 * yyyyMMddHHmm
		   		 * Long
		   		 */
				FORMATSTR.put(1,"yyyy-MM-dd");
				FORMATSTR.put(2,"yyyy-MM-dd HH:mm:ss");
				FORMATSTR.put(3,"yyyy-MM-dd HH:mm");
				FORMATSTR.put(4,"yyyy/MM/dd");
				FORMATSTR.put(5,"yyyy/MM/dd HH:mm:ss");
				FORMATSTR.put(6,"yyyy/MM/dd HH:mm");
				FORMATSTR.put(7,"yyyyMMdd");
				FORMATSTR.put(8,"yyyyMMddHHmmss");
				FORMATSTR.put(9,"yyyyMMddHHmm");
				FORMATSTR.put(10,"LONG");
				FORMATSTR.put(11,"HH:mm");
				FORMATSTR.put(12,"HH:mm:ss");

		 }
		/**
		 *
		 * @Description: TODO
		 * @param: @param dateString
		 * @param: @param format
		 * @param: @return
		 * @return: Date
		 * throws:
		 *
		 */
		public static Date formatDateByFormat(String dateString,String format) {
			if("LONG".equals(format)) {
				return new Date(Long.parseLong(dateString));
			}else {
				return getDate(dateString, format);
			}
		}

		public static String backformatDateByFormat(Date date,String format) {
			if("LONG".equals(format)) {
				return date.getTime()+"";
			}else {
				return backFormatDate(date, format);
			}
		}
		private static GregorianCalendar grc = new GregorianCalendar();

		private static SimpleDateFormat initFormat(String format) {
			SimpleDateFormat mDateFormat = new SimpleDateFormat(format);
			return mDateFormat;
		}
		/**
		 * 获取时间 时间不正确，返回当前的时间
		 *
		 * @param date
		 * @param format
		 * @return
		 * @throws ParseException
		 */
		public static Date getDate(String date, String format) {
			try {
				return initFormat(format).parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return null;
		}
		public static String backFormatDate(Date date, String format) {
			return initFormat(format).format(date);
		}
		/***
		 * 根据类型获取需要的起止时间
		 * Description: TODO
		 * param: @param dataType
		 * param: @return
		 * return: Map<String,Date>
		 * throws:
		 *
		 */
		public static Map<String, Date> getDateByDataType(Integer dataType){
			Map<String, Date> map=new HashMap<String, Date>();
			switch(dataType) {
				case 0:
					break;
				case 1:
					map=getWeekDayDate();
					break;
				case 2:
					map=getMonthDateasDate();
					break;
				case 3:
					map=getWeekDayDate(new Date());
					break;
				case 4:
					map=getSpaceDayDate(new Date(), 30);
					break;
				default:
					break;
			}

			return map;
		}


		public static Date parseMMddyyyyHHmmss(String dateText) {
			SimpleDateFormat MMddyyyyHHmmss = new SimpleDateFormat(MMddyyyyHms);
			try {
				return MMddyyyyHHmmss.parse(dateText);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		}

		public static String hh(Date date) {
			SimpleDateFormat HH = new SimpleDateFormat("HH");
			return HH.format(date);
		}

		public static String standardFmtsS(Date date) {
			SimpleDateFormat standardFmtsS = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_s);
			return standardFmtsS.format(date);
		}


		public static String hhmmss(Date date) {
			SimpleDateFormat HH_MM_SS = new SimpleDateFormat("HH:mm:ss");
			return HH_MM_SS.format(date);
		}

		public static Date parseDatestandardFmt(String dateText) {
			SimpleDateFormat standardFmt = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			try {
				return standardFmt.parse(dateText);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		}

		public static Date getMonthtoDate(Date date, Integer month) {
			grc.setTime(date);
			grc.add(Calendar.MONTH, month);
			return new Timestamp(grc.getTimeInMillis());
		}

		// 获取上个月的最后一天
		public static Date getBeforeMonthEndDay() {
			Calendar cal = Calendar.getInstance();
			// 调到上个月
			cal.add(Calendar.MONTH, -1);
			// 得到一个月最最后一天日期(31/30/29/28)
			int MaxDay = cal.getMaximum(Calendar.DAY_OF_MONTH);
			// 按你的要求设置时间
			cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), MaxDay, 23, 59, 59);
			// 按格式输出
			return cal.getTime();
			// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
			// System.out.println(sdf.format(cal.getTime()));
		}

		public static String formatHHmmss(Date date) {
			SimpleDateFormat HHmmss = new SimpleDateFormat("HH:mm:ss");
			return HHmmss.format(date);
		}
		public static String formatMM_dd_yyyy_hhmm(Date date) {
			SimpleDateFormat MM_dd_yyyy_hhmm = new SimpleDateFormat("MM/dd/yyyyHHmm");
			return MM_dd_yyyy_hhmm.format(date);
		}
		public static String formatyyyyMMddHH(Date date) {
			SimpleDateFormat yyyyMMddHH = new SimpleDateFormat("yyyyMMddHH");
			return yyyyMMddHH.format(date);
		}

		public String getyyyyMMdd(Date date, int fixedHours) {
			SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(10, fixedHours);
			log.debug((new StringBuilder("修正小时数:")).append(fixedHours).toString());
			String dateStr = yyyyMMdd.format(c.getTime());
			log.debug((new StringBuilder("修正后时间:")).append(c.getTime().toString()).toString());
			return dateStr;
		}

		public static Date getFirstDayOfMonth(Date date, Integer month, Integer year) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			month = Integer.valueOf(month != null && month.intValue() >= 0 && month.intValue() <= 11 ? month.intValue() : calendar.get(2));
			year = Integer.valueOf(year != null && year.intValue() >= 0 ? year.intValue() : calendar.get(1));
			calendar.set(1, year.intValue());
			calendar.set(2, month.intValue());
			calendar.set(5, 1);
			calendar.set(11, 0);
			calendar.set(12, 0);
			calendar.set(13, 0);
			calendar.set(14, 0);
			return calendar.getTime();
		}

		public static Date getLastDayOfMonth(Date date) {
			final Calendar cal = Calendar.getInstance();
	        cal.setTime(date);
	        final int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	        cal.set(Calendar.DAY_OF_MONTH, last);
	        return cal.getTime();
		}

		public static Date getLongAgo() {
			return new Date(0);
		}






		public static Timestamp now() {
			Calendar currDate = Calendar.getInstance();
			return new Timestamp((currDate.getTime()).getTime());
		}

		public static Date getToday() {
			Calendar c = Calendar.getInstance();
			c.setTime(c.getTime());
			c.set(11, 0);
			c.set(12, 0);
			c.set(13, 0);
			return c.getTime();
		}

		public static Date getToday(Date date) {
			Calendar c = Calendar.getInstance();
			c.setTime(c.getTime());
			c.set(11, 0);
			c.set(12, 0);
			c.set(13, 0);
			return c.getTime();
		}

		public static Date getTomorrow() {
			Calendar c = Calendar.getInstance();
			c.setTime(c.getTime());
			c.add(6, 1);
			c.set(11, 0);
			c.set(12, 0);
			c.set(13, 0);
			return c.getTime();
		}

		public static Date getTomorrow(Date date) {
			Calendar c = Calendar.getInstance();
			c.setTime(c.getTime());
			c.add(6, 1);
			c.set(11, 0);
			c.set(12, 0);
			c.set(13, 0);
			return c.getTime();
		}

		public static Date getYesterday(Date date) {
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(6, -1);
			c.set(11, 0);
			c.set(12, 0);
			c.set(13, 0);
			return c.getTime();
		}

		public static Date getOneDayAgo(Date date) {
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(Calendar.DAY_OF_MONTH, -1);
			return c.getTime();
		}

		public static Timestamp convertToTimestamp(Date date) {
			if (date != null) {
				return new Timestamp(date.getTime());
			}
			return null;
		}

		public static String fmthhmmss(Date date) {
			SimpleDateFormat HH_MM_SS = new SimpleDateFormat("HH:mm:ss");
			return HH_MM_SS.format(date);
		}

		public static String fmthh(Date date) {
			SimpleDateFormat HH = new SimpleDateFormat("HH");
			return HH.format(date);
		}

		public static String fmtyyyyMMddHHmmss(Date date) {
			SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat(YYYYMMDDHHmmss);
			return yyyyMMddHHmmss.format(date);
		}
		public static String fmtyyyyMMdd_HHmmss(Date date) {
			SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat(yyyyMMdd_HHmmss);
			return yyyyMMddHHmmss.format(date);
		}
		public static String fmtDateForBetRecods(Date date) {
			SimpleDateFormat standardFmt = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			return standardFmt.format(date);
		}

		public static String fmtyyyyMMdd(Date date) {
			SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
			return yyyyMMdd.format(date);
		}

		public static String fmtyyyy_MM_dd(Date date) {
			SimpleDateFormat yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
			return yyyy_MM_dd.format(date);
		}

		public static String fmtyyMMdd(Date date) {
			SimpleDateFormat yyMMdd = new SimpleDateFormat("yyMMdd");
			return yyMMdd.format(date);
		}

		public static String formatDateForStandard(Date date) {
			SimpleDateFormat standardFmt = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			return standardFmt.format(date);
		}

		public static Timestamp convertTimestamp(Date date) {
			Calendar currDate = Calendar.getInstance();
			currDate.setTime(date);
			return new Timestamp(currDate.getTimeInMillis());
		}

		public static Date parseDate(String fmt, String dateText) {
			try {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fmt);
				return simpleDateFormat.parse(dateText);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		}

		public static Date parseDateRemoteDetails(String dateText) {
			SimpleDateFormat standardFmtRemoteDetails = new SimpleDateFormat(MMddyyyyHms);
			try {
				return standardFmtRemoteDetails.parse(dateText);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		}

		public static String getOneHourAgoFormat(Date date) {
			SimpleDateFormat standardFmt = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(Calendar.HOUR_OF_DAY, -1);
			return standardFmt.format(c.getTime());
		}

		public static Date parseDateForStandard(String dateText) {
			SimpleDateFormat standardFmt = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			try {
				return standardFmt.parse(dateText);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		}

		public static Date parseDateyyyyMMddHHmmss(String dateText) {
			SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat(YYYYMMDDHHmmss);
			try {
				return yyyyMMddHHmmss.parse(dateText);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		}

		/**
		 *
		 * @param day
		 *            时间相加(天)
		 * @return
		 */
		public static String getMontHreduce(Date date, Integer day) {
			SimpleDateFormat standardFmt = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			grc.setTime(date);
			grc.add(Calendar.DAY_OF_MONTH, day);
			return standardFmt.format(grc.getTime());
		}

		/**
		 *
		 * @param date
		 * @param second        时间相加(秒 )
		 * @return
		 */
		public static Date getMontSecond(Date date, Integer second) {
			grc.setTime(date);
			grc.add(Calendar.SECOND, second);
			return grc.getTime();
		}

		/**
		 *
		 * @param day
		 *            时间相加(天)
		 * @return Date
		 */
		public static Date getMontToDate(Date date, Integer day) {
			grc.setTime(date);
			grc.add(Calendar.DAY_OF_MONTH, day);
			return new Timestamp(grc.getTimeInMillis());
		}

		/**
		 *
		 * Description: TODO
		 * param: @param date
		 * param: @return
		 * return: String
		 * throws:
		 *
		 */
		public static String getYear(Date date) {
			grc.setTime(date);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			return sdf.format(date);
		}

		/**
		 *
		 * Description: TODO
		 * param: @param date
		 * param: @return
		 * return: String
		 * throws:
		 *
		 */
		public static String getMonth(Date date) {
			grc.setTime(date);
			SimpleDateFormat sdf = new SimpleDateFormat("M");
			return sdf.format(date);
		}

		/**
		 * @return
		 */
		public static String getDay(Date date) {
			grc.setTime(date);
			SimpleDateFormat sdf = new SimpleDateFormat("d");
			return sdf.format(date);
		}

		// 上周 的一天
		@SuppressWarnings("static-access")
		public static Date getLastWeek(Date date, Integer index) {
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(Calendar.DATE, -7);
			c.set(Calendar.DAY_OF_WEEK, index);
			grc.setTime(c.getTime());
			grc.add(Calendar.DAY_OF_MONTH, 1);
			return new Timestamp(grc.getTimeInMillis());
		}





		// 本周 的一天
		@SuppressWarnings("static-access")
		public static Date getThisWeek(Date date, Integer index) {
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.set(Calendar.DAY_OF_WEEK, index);
			grc.setTime(c.getTime());
			grc.add(Calendar.DAY_OF_MONTH, 1);
			return new Timestamp(grc.getTimeInMillis());
		}


		// 下周的一天
		@SuppressWarnings("static-access")
		public static Date getNextWeek(Date date, Integer index) {
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(Calendar.DATE, 7);
			c.set(Calendar.DAY_OF_WEEK, index);
			grc.setTime(c.getTime());
			grc.add(Calendar.DAY_OF_MONTH, 1);
			return new Timestamp(grc.getTimeInMillis());
		}

		public static Integer getWeekNumber(Date date) {
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
			switch (dayOfWeek) {
			case 1:
				return 7;
			case 2:
				return 1;
			case 3:
				return 2;
			case 4:
				return 3;
			case 5:
				return 4;
			case 6:
				return 5;
			case 7:
				return 6;
			}
			return null;
		}

		/**
		 * 获取给定时间所在月的第一天的日期
		 */
		public static Date getMonthStartdDate(Date date) {
			Date firstDateOfMonth;
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			// 得到当天是这月的第几天
			int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
			// 减去dayOfMonth,得到第一天的日期，因为Calendar用０代表每月的第一天，所以要减一
			c.add(Calendar.DAY_OF_MONTH, -(dayOfMonth - 1));
			firstDateOfMonth = c.getTime();
			return firstDateOfMonth;
		}
		/***
		 * 获取给定时间月的第一天时间返回yyyy-MM-dd字符串
		 * Description: TODO
		 * param: @param date
		 * param: @return
		 * return: String
		 * throws:
		 *
		 */
		public static String getMonthStart(Date date) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.MONTH, -1);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			return sdf.format(calendar.getTime());
		}
		/**
		 * 获取给定时间月最后一天时间返回yyyy-MM-dd字符串
		 * Description: TODO
		 * param: @param date
		 * param: @return
		 * return: String
		 * throws:
		 *
		 */
		public static String getMonthEnd(Date date) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.add(Calendar.DATE, 30);
			return sdf.format(calendar.getTime());
		}
		/****
		 * 获取给定时间下月第一天
		 * Description: TODO
		 * param: @param date
		 * param: @return
		 * return: Date
		 * throws:
		 *
		 */
		public static Date nextMonthFirstDate(Date date) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.add(Calendar.MONTH, 1);
			return calendar.getTime();
		}


		/****
		 * 获取给定时间下月第一天
		 * Description: TODO
		 * param: @param date
		 * param: @return
		 * return: Date
		 * throws:
		 *
		 */
		public static Date getNextMonthStartdDate(Date date) {
			Date firstDateOfMonth;
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			// 得到当天是这月的第几天
			int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
			// 减去dayOfMonth,得到第一天的日期，因为Calendar用０代表每月的第一天，所以要减一
			c.add(Calendar.DAY_OF_MONTH, -(dayOfMonth - 1));
			firstDateOfMonth = c.getTime();
			return firstDateOfMonth;
		}

		/**
		 * 获取给定时间所在月的最后一天的日期
		 */
		public static Date getMonthEndDate(Date date) {
			Date lastDateOfMonth;
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			// calendar.getActualMaximum(Calendar.DAY_OF_MONTH)得到这个月有几天
			c.add(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH) - 1);
			lastDateOfMonth = c.getTime();
			return lastDateOfMonth;
		}

		/**
		 * 获取给定时间的差异 返回年数
		 */
		public static int getDistanceyear(Date start, Date end) {
			SimpleDateFormat standardFmt = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			String startdate = fmtDateForBetRecods(start);
			String enddate = fmtDateForBetRecods(end);
			int year = 0;
			try {
				Long beginL = standardFmt.parse(startdate).getTime();
				Long endL = standardFmt.parse(enddate).getTime();
				year = (int) (endL - beginL) / 1000 / 60 / 60 / 24 / 356;
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return year;
		}

		/**
		 * 获取给定时间的差异 返回月数
		 */
		public static int getDistancemonth(Date start, Date end) {
			SimpleDateFormat standardFmt = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			String startdate = fmtDateForBetRecods(start);
			String enddate = fmtDateForBetRecods(end);
			int month = 0;
			try {
				Long beginL = standardFmt.parse(startdate).getTime();
				Long endL = standardFmt.parse(enddate).getTime();
				month = (int) (endL - beginL) / 1000 / 60 / 60 / 24 / 30;
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return month;
		}

		/**
		 * 获取给定时间的差异 返回天数
		 */
		public static int getDistancedate(Date start, Date end) {
			SimpleDateFormat standardFmt = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			String startdate = fmtDateForBetRecods(start);
			String enddate = fmtDateForBetRecods(end);
			int date = 0;
			try {
				Long beginL = standardFmt.parse(startdate).getTime();
				Long endL = standardFmt.parse(enddate).getTime();
				date = (int) (endL - beginL) / 1000 / 60 / 60 / 24;
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return date;
		}

		/**
		 * 获取给定时间的差异 返回小时
		 */
		public static int getDistancehour(Date start, Date end) {
			SimpleDateFormat standardFmt = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			String startdate = fmtDateForBetRecods(start);
			String enddate = fmtDateForBetRecods(end);
			int hour = 0;
			try {
				Long beginL = standardFmt.parse(startdate).getTime();
				Long endL = standardFmt.parse(enddate).getTime();
				hour = (int) (endL - beginL) / (1000 * 60 * 60);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return hour;

		}

		/**
		 * 获取给定时间的差异 返回分钟
		 */
		public static int getDistanceMinute(Date start, Date end) {
			SimpleDateFormat standardFmt = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			String startdate = fmtDateForBetRecods(start);
			String enddate = fmtDateForBetRecods(end);
			int Minute = 0;
			try {
				Long beginL = standardFmt.parse(startdate).getTime();
				Long endL = standardFmt.parse(enddate).getTime();
				Minute = (int) (endL - beginL) / (1000 * 60);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return Minute;
		}

		/**
		 * 获取给定时间的差异 返回秒数
		 */
		public static int getDistanceSecond(Date start, Date end) {
			SimpleDateFormat standardFmt = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			String startdate = fmtDateForBetRecods(start);
			String enddate = fmtDateForBetRecods(end);
			int Second = 0;
			try {
				Long beginL = standardFmt.parse(startdate).getTime();
				Long endL = standardFmt.parse(enddate).getTime();
				Second = (int) (endL - beginL) / (1000);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return Second;
		}

		/**
		 * 获取给定时间的差异 返回毫秒数
		 */
		public static int getMillisecond(Date start, Date end) {
			SimpleDateFormat standardFmt = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			String startdate = fmtDateForBetRecods(start);
			String enddate = fmtDateForBetRecods(end);
			int Millisecond = 0;
			try {
				Long beginL = standardFmt.parse(startdate).getTime();
				Long endL = standardFmt.parse(enddate).getTime();
				Millisecond = (int) (endL - beginL);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return Millisecond;
		}

		public static Date getYesterBeginday(Date date) {
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(6, -1);
			c.set(11, 0);
			c.set(12, 0);
			c.set(13, 0);
			return c.getTime();
		}

		public static Date getOneDayBeginDate(Date d) {
			Calendar c = Calendar.getInstance();
			c.setTime(d);
			c.set(11, 0);
			c.set(12, 0);
			c.set(13, 0);
			return c.getTime();
		}

		public static Date getOneDayEndDate(Date d) {
			Calendar c = Calendar.getInstance();
			c.setTime(d);
			c.set(11, 23);
			c.set(12, 59);
			c.set(13, 59);
			return c.getTime();
		}

		public static Date getYesterEndday(Date date) {
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(6, -1);
			c.set(11, 23);
			c.set(12, 59);
			c.set(13, 59);
			return c.getTime();
		}

		public static Date parseyyyy_MM_dd(String dateText) {
			SimpleDateFormat yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
			try {
				return yyyy_MM_dd.parse(dateText);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return null;
		}

		public static Date parseyyyy_MM(String dateText) {
			SimpleDateFormat yyyy_MM = new SimpleDateFormat("yyyy-MM");
			try {
				return yyyy_MM.parse(dateText);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return null;
		}

		public static Date getDate_addMh(Date date, int addnum) {
			Date d = null;
			try {
				Calendar c = Calendar.getInstance();
				c.setTime(date);
				c.add(2, addnum);
				d = c.getTime();
			} catch (Exception e) {
				e.printStackTrace();
			}

			return d;
		}

		public static String fmtyyyy_MM(Date date) {
			String str = null;
			try {
				SimpleDateFormat yyyy_MM = new SimpleDateFormat("yyyy-MM");
				str = yyyy_MM.format(date);
			} catch (Exception e) {
				e.printStackTrace();
				str = null;
			}

			return str;
		}

		public static Date getYyyy_MM(Date d) {
			try {
				return parseyyyy_MM(fmtyyyy_MM(d));
			} catch (Exception e) {
			}
			return null;
		}

		public static int getDistancemonth1(Date start, Date end) {
			int month = 0;
			try {
				Calendar cals = Calendar.getInstance();
				Calendar cale = Calendar.getInstance();
				cale.setTime(end);
				cals.setTime(start);
				month = cale.get(1) * 12 + cale.get(2) - (cals.get(1) * 12 + cals.get(2));
			} catch (Exception e) {
				e.printStackTrace();
			}
			return month;
		}

		public static Date getTodayBeginDate(Date d) {
			Calendar c = Calendar.getInstance();
			c.setTime(d);
			c.set(11, 0);
			c.set(12, 0);
			c.set(13, 0);
			return c.getTime();
		}

		public static Date getTodayEndDate(Date d) {
			Calendar c = Calendar.getInstance();
			c.setTime(d);
			c.set(11, 23);
			c.set(12, 59);
			c.set(13, 59);
			return c.getTime();
		}

		public static Date getWeekBegin(Date date) {
			Calendar cd = Calendar.getInstance();
			cd.setTime(date);

			int dayOfWeek = cd.get(7);
			int mondayPlus = 1 - dayOfWeek;

			GregorianCalendar currentDate = new GregorianCalendar();
			currentDate.add(5, mondayPlus);
			Date monday = currentDate.getTime();
			Calendar cal = Calendar.getInstance();
			cal.setTime(monday);
			cal.set(11, 0);
			cal.set(12, 0);
			cal.set(13, 0);

			return cal.getTime();
		}

		public static Date getMonthBegin(Date date) {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.set(5, 1);
			cal.set(11, 0);
			cal.set(12, 0);
			cal.set(13, 0);

			return cal.getTime();
		}

		public static Date getYearBegin(int year) {
			Calendar calendar = Calendar.getInstance();
			calendar.clear();
			calendar.set(1, year);
			Date currYearFirst = calendar.getTime();
			return currYearFirst;
		}

		public static Date getYearBegin(Date date) {
			Calendar calendar = Calendar.getInstance();
			calendar.clear();
			DateFormat df = new SimpleDateFormat("yyyy");
			calendar.set(1, Integer.valueOf(df.format(date)).intValue());
			Date currYearFirst = calendar.getTime();
			return currYearFirst;
		}



		public static Date getDate_addHr(Date date, int addnum) {
			Date d = null;
			try {
				Calendar c = Calendar.getInstance();
				c.setTime(date);
				c.add(11, addnum);
				d = c.getTime();
			} catch (Exception e) {
				e.printStackTrace();
			}

			return d;
		}


		public static int getDaysBetweenTwoDates(Date start, Date end) {
			String startdate = fmtyyyy_MM_dd(start);
			String enddate = fmtyyyy_MM_dd(end);
			int date = 0;
			try {
				Long beginL = Long.valueOf(parseyyyy_MM_dd(startdate).getTime());
				Long endL = Long.valueOf(parseyyyy_MM_dd(enddate).getTime());
				date = new Long((endL.longValue() - beginL.longValue()) / 86400000L).intValue();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return date;
		}

		public static Date parseDateForStandard1(String dateText) {
			SimpleDateFormat yyyy_MM = new SimpleDateFormat("yyyy-MM");
			try {
				return yyyy_MM.parse(dateText);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return null;
		}

		public static String formatyyyyMM(Date date) {
			SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMM");
			return yyyyMM.format(date);
		}
		/**获取世界时间***/
		public static Date getGMTTime(Date date){
			Date date1=DateUtils.addHours(date, -8);
			return date1;
		}
		/**世界时间获取然后转换为字符串**/
		public static String getGMTTimeString(Date date){
			Date date1=DateUtils.addHours(date, -8);
	        SimpleDateFormat foo = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
			String time = foo.format(date1);
			String en=null;
			try {
				en=URLEncoder.encode(time, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			return en;
		}

		/**传入（时间Date）世界时间进行转换*******/
		public static String encodeDate(Date date){
			SimpleDateFormat foo = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
			String time = foo.format(date);
			String en=null;
			try {
				en=URLEncoder.encode(time, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			return en;
		}

		/**世界时间获取然后转换为字符串**/
		public static String getGMTTimeStringyyyymmddhhmmss(){
			Calendar cal = Calendar.getInstance();
			int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
			int dstOffset = cal.get(Calendar.DST_OFFSET);
			cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
			long mills = cal.getTimeInMillis();
			cal.setTimeInMillis(mills);
	        Date date= cal.getTime();
	        SimpleDateFormat foo = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			String time = foo.format(date);
			return time;
		}




		/**获取昨天时间*中国时间或者*yyyy-MM-dd HH:mm:ss*/
		public static String getyesterDayString(Date dates){
			Date date=getYesterday(dates);
			SimpleDateFormat foo = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
	        String dstr=foo.format(date);
	        return dstr;
		}

		/***世界时间转 中国时间*********/
		public static Date getChinaDateByGTMTim(String GtmTime){
			SimpleDateFormat foo = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			Date date=new Date();
			try {
				date = foo.parse(GtmTime);
				date=DateUtils.addHours(date, 8);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return date;
		}
		/****
		 * 获取年月日
		 * @param date
		 * @return
		 */
		public static String getYYYYMMDD(Date date){
			SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat(YYYYMMDDHHmmss);
			String datestr=yyyyMMddHHmmss.format(date);
			String yyyymmdd=datestr.substring(0, 8);
			return yyyymmdd;
		}
		/****
		 * 获取时
		 * @param date
		 * @return
		 */
		public static String getHH(Date date){
			SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat(YYYYMMDDHHmmss);
			String datestr=yyyyMMddHHmmss.format(date);
			String yyyymmdd=datestr.substring(8, 10);
			return yyyymmdd;
		}
		/****
		 * 获取分
		 * @param date
		 * @return
		 */
		public static String getmm(Date date){
			SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat(YYYYMMDDHHmmss);
			String datestr=yyyyMMddHHmmss.format(date);
			String yyyymmdd=datestr.substring(10, 12);
			return yyyymmdd;
		}
		/***
		 * 获取date时间
		 * @param dateStr
		 * @return
		 */
		public static Date getDateYYYYMMDD(String dateStr,Date dates){
			SimpleDateFormat yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
			Date date=dates;
			try {
				date=yyyy_MM_dd.parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return date;
		}
		/***
		 *获取本月开始时间和结束时间
		 * @return
		 */
		public static Map<String,String> getMonthDate(){
			  Map<String,String> map = new HashMap<String,String>();
			  // 获取Calendar
			  Calendar calendar = Calendar.getInstance();
			   DateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
			  // 设置时间,当前时间不用设置
			  // calendar.setTime(new Date());
			   calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
			   map.put("monthF", format.format(calendar.getTime()));
			   /*print("*********得到本月的最小日期**********"+format.format(calendar.getTime()));*/
			 // 设置日期为本月最大日期
			   calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
			 // 打印
			   map.put("monthL", format.format(calendar.getTime()));
			   /*print("*********得到本月的最大日期**********"+format.format(calendar.getTime()));*/
			   return map;
			 }
		/***
		 *获取本月开始时间和结束时间
		 * @return
		 */
		public static Map<String,Date> getMonthDateasDate(){
			  Map<String,Date> map = new HashMap<String,Date>();
			  // 获取Calendar
			  Calendar calendar = Calendar.getInstance();
			  // 设置时间,当前时间不用设置
			  // calendar.setTime(new Date());
			   calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
			   map.put("start", calendar.getTime());
			   /*print("*********得到本月的最小日期**********"+format.format(calendar.getTime()));*/
			 // 设置日期为本月最大日期
			   calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
			 // 打印
			   map.put("end", calendar.getTime());
			   /*print("*********得到本月的最大日期**********"+format.format(calendar.getTime()));*/
			   return map;
			 }
		/**
		 * 获取本周第一天和本周最后一天
		 * @return
		 */
		public static Map<String,String> getWeekDay() {
			  Map<String,String> map = new HashMap<String,String>();
			  Calendar cal =Calendar.getInstance();
			  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
			  cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //获取本周一的日期
			  map.put("mon", df.format(cal.getTime()));
			  //这种输出的是上个星期周日的日期，因为老外那边把周日当成第一天
			  cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
			  //增加一个星期，才是我们中国人理解的本周日的日期
			  cal.add(Calendar.WEEK_OF_YEAR, 1);
			  SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
			  map.put("sun", df2.format(cal.getTime()));
			  return map;
			 }
		/**
		 *
		 * 获取本周第一天和最后一天
		 * Description: TODO
		 * param: @return
		 * return: Map<String,Date>
		 * throws:
		 *
		 */
		public static Map<String,Date> getWeekDayDate() {
			  Map<String,Date> map = new HashMap<String,Date>();
			  Calendar cal =Calendar.getInstance();
			  cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //获取本周一的日期
			  map.put("start", cal.getTime());
			  //这种输出的是上个星期周日的日期，因为老外那边把周日当成第一天
			  cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
			  //增加一个星期，才是我们中国人理解的本周日的日期
			  cal.add(Calendar.WEEK_OF_YEAR, 1);
			  map.put("end", cal.getTime());
			  return map;
			 }
		/**
		 * 取最近七天
		 * Description: TODO
		 * param: @param now
		 * param: @return
		 * return: Map<String,Long>
		 * throws:
		 *
		 */
		public static Map<String,Long> getWeekDayLong(Date now) {
			  Map<String,Long> map = new HashMap<String,Long>();

			  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
			  SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
			  Date start=addDays(now, -7);
			  Long startLong=start.getTime();
			  Long endLong=now.getTime();
			try {
				startLong = df.parse(df.format(start)).getTime();
				endLong=df2.parse(df2.format(now)).getTime();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			  map.put(startStr, startLong);
			  map.put(endStr, endLong);
			  return map;
			 }

			/****
			 * 获取最近指定天数起止时间
			 * Description: TODO
			 * param: @param now
			 * param: @param days
			 * param: @return
			 * return: Map<String,Date>
			 * throws:
			 *
			 */
			public static Map<String,Date> getSpaceDayDate(Date now,Integer days) {
			  Map<String,Date> map = new HashMap<String,Date>();
			  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
			  SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
			  Date start=addDays(now, days*-1);
			  Date startLong=start;
			  Date endLong=now;
			try {
				startLong = df.parse(df.format(start));
				endLong=df2.parse(df2.format(now));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			  map.put(startStr, startLong);
			  map.put(endStr, endLong);
			  return map;
			 }
		    /*****
		     * 获取最近七天起止时间
		     * Description: TODO
		     * param: @param now
		     * param: @return
		     * return: Map<String,Date>
		     * throws:
		     *
		     */
			public static Map<String,Date> getWeekDayDate(Date now) {
			  Map<String,Date> map = new HashMap<String,Date>();
			  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
			  SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
			  Date start=addDays(now, -7);
			  Date startLong=start;
			  Date endLong=now;
			try {
				startLong = df.parse(df.format(start));
				endLong=df2.parse(df2.format(now));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			  map.put(startStr, startLong);
			  map.put(endStr, endLong);
			  return map;
			 }

		/**
		 * 获取传入时间的凌晨时间
		 * @return
		 */
		public static Date getZeroTime(Date now){
			Calendar calendar = Calendar.getInstance();
	    	calendar.setTime(now);
	    	calendar.set(Calendar.HOUR_OF_DAY,0);
	        calendar.set(Calendar.MINUTE,0);
	        calendar.set(Calendar.SECOND,0);
	        calendar.set(Calendar.MILLISECOND,0);//如不设置存入mysql时会被自动进位
			return calendar.getTime();
		}
		/**
		 * 获取传入时间当天最后一秒 23:59:59.0
		 * @return
		 */
		public static Date get24Time(Date now){
			Calendar calendar = Calendar.getInstance();
	    	calendar.setTime(now);
	    	calendar.set(Calendar.HOUR_OF_DAY,23);
	        calendar.set(Calendar.MINUTE,59);
	        calendar.set(Calendar.SECOND,59);
	        calendar.set(Calendar.MILLISECOND,0);//如不设置存入mysql时会被自动进位
			return calendar.getTime();
		}
		public static Map<Integer,Date> getstartAndendDate(Date date){
			SimpleDateFormat standardFmt = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			Map<Integer,Date> map=new HashMap<Integer,Date>();
			String thisDate=standardFmt.format(date);
			String startTime=thisDate.substring(0, 11)+"00:00:00";
			String endTime=thisDate.substring(0, 11)+"23:59:59";
			Date start=null;
			Date end=null;
			try {
				start = standardFmt.parse(startTime);
				end=standardFmt.parse(endTime);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			map.put(1, start);
			map.put(2, end);
			return map;
		}

		 /**
	     * 计算两个日期之间相差的天数
	     * @param smdate 较小的时间
	     * @param bdate  较大的时间
	     * @return 相差天数  算上今天
	     * @throws ParseException
	     */
	    public static int daysBetween(Date smdate,Date bdate){
	        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	        try {
				smdate=sdf.parse(sdf.format(smdate));
				 bdate=sdf.parse(sdf.format(bdate));
			} catch (ParseException e) {
				e.printStackTrace();
			}
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(smdate);
	        long time1 = cal.getTimeInMillis();
	        cal.setTime(bdate);
	        long time2 = cal.getTimeInMillis();
	        long between_days=(time2-time1)/(1000*3600*24);

	       return Integer.parseInt(String.valueOf(between_days))+1;
	    }
	    /**
	     * 计算两个日期之间相差的天数
	     * @param smdate 较小的时间
	     * @param bdate  较大的时间
	     * @return 相差天数  不算今天
	     * @throws ParseException
	     */
	    public static int daysBetweenNotthis(Date smdate,Date bdate){
	        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	        try {
				smdate=sdf.parse(sdf.format(smdate));
				 bdate=sdf.parse(sdf.format(bdate));
			} catch (ParseException e) {
				e.printStackTrace();
			}
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(smdate);
	        long time1 = cal.getTimeInMillis();
	        cal.setTime(bdate);
	        long time2 = cal.getTimeInMillis();
	        long between_days=(time2-time1)/(1000*3600*24);

	       return Integer.parseInt(String.valueOf(between_days));
	    }


	    /**
	     * 根据URL和格式化类型获取时间
	     *
	     * @param webuUrl
	     *            网络URL
	     * @param format
	     *            格式
	     * @return
	     */
	    public static Date getWebsiteDatetimeDate(String webuUrl, String format) {
	        try {
	            // 判断当前是否传入URL
	            if (!StringUtils.isEmpty(webuUrl)) {
	                URL url = new URL(webuUrl);// 获取url对象
	                URLConnection uc = url.openConnection();// 获取生成连接对象
	                uc.connect();// 发出连接请求
	                long ld = uc.getDate();// 读取网站日期时间
	                Date date = new Date(ld);// 转化为时间对象
	                return date;
	            } else {
	                System.out.println("URL Error!!!");
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return null;

	    }

	    /**获取现在的中国时间****/
		public static Date getChinaDate(){
			 Calendar cal = Calendar.getInstance();
		        // 设置格式化的SimpleDateFormat对象，指定中国语言环境
		        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		        // 创建时区（TimeZone）对象，设置时区为“亚洲/重庆"
		        TimeZone TZ = TimeZone.getTimeZone("Asia/Chongqing");
		        // 将SimpleDateFormat强制转换为DateFormat
		        DateFormat df = null;
		        try
		        {
		            df = (DateFormat)sdf;
		        }
		        catch(Exception E)
		        {
		            E.printStackTrace();
		        }
		        // 为DateFormat对象设置时区
		        df.setTimeZone(TZ);
		        // 获取时间表达式
		        return cal.getTime();
		}

		 public static Date getBeginTime(int year, int month) {
		        YearMonth yearMonth = YearMonth.of(year, month);
		        LocalDate localDate = yearMonth.atDay(1);
		        LocalDateTime startOfDay = localDate.atStartOfDay();
		        ZonedDateTime zonedDateTime = startOfDay.atZone(ZoneId.of("Asia/Shanghai"));

		        return Date.from(zonedDateTime.toInstant());
		    }

		    public static Date getEndTime(int year, int month) {
		        YearMonth yearMonth = YearMonth.of(year, month);
		        LocalDate endOfMonth = yearMonth.atEndOfMonth();
		        LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
		        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
		        return Date.from(zonedDateTime.toInstant());
		    }
		    //获取时间是该年的第几周
		   public static int getDateWeek(Date date){
			   Calendar calendar = Calendar.getInstance();
			   calendar.setFirstDayOfWeek(Calendar.MONDAY);
			   calendar.setTime(date);
			   return calendar.get(Calendar.WEEK_OF_YEAR)-1;
		   }

		   //
		   public static String getLastDayOfMonth(int year,int month){
	           Calendar calendar = Calendar.getInstance();
	           calendar.set(Calendar.YEAR , year);
	           calendar.set(Calendar.MONTH , month - 1);
	           calendar.set(Calendar.DATE , 1);
	           calendar.add(Calendar.MONTH, 1);
	           calendar.add(Calendar.DAY_OF_YEAR , -1);
	           return calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" +
	                  calendar.get(Calendar.DAY_OF_MONTH);
	       }

	       /**
	        * get Calendar of given year
	        * @param year
	        * @return
	        */
	       private static Calendar getCalendarFormYear(int year){
	           Calendar cal = Calendar.getInstance();
	           cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
	           cal.set(Calendar.YEAR, year);
	           return cal;
	       }

	       /**
	        * get start date of given week no of a year
	        * @param year
	        * @param weekNo
	        * @return
	        */
	       public static String getStartDayOfWeekNo(int year,int weekNo){
	           Calendar cal = getCalendarFormYear(year);
	           cal.set(Calendar.WEEK_OF_YEAR, weekNo);
	           return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" +
	                  cal.get(Calendar.DAY_OF_MONTH);

	       }

	       /**
	        * get the end day of given week no of a year.
	        * @param year
	        * @param weekNo
	        * @return
	        */
	       public static String getEndDayOfWeekNo(int year,int weekNo){
	           Calendar cal = getCalendarFormYear(year);
	           cal.set(Calendar.WEEK_OF_YEAR, weekNo);
	           cal.add(Calendar.DAY_OF_WEEK, 6);
	           return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" +
	                  cal.get(Calendar.DAY_OF_MONTH);
	       }

			/**
			 * 判断时间是本周第几天
			 * @param tmpDate
			 * @return
			 * @throws Throwable
			 */
			public static Integer dayForWeek(Date tmpDate) {
				Calendar cal = Calendar.getInstance();

				String[] weekDays = { "7", "1", "2", "3", "4", "5", "6" };

				try {

					cal.setTime(tmpDate);

				} catch (Exception e) {

					e.printStackTrace();

				}
				// 指示一个星期中的某天。
				int w = cal.get(Calendar.DAY_OF_WEEK) - 1;

				if (w < 0){
					w = 0;
				}
				return Integer.parseInt(weekDays[w]);

			}
			/**
			 * 传入时间判断  是否在区间内
			 * @param nowTimeStr       HH:mm:ss
			 * @param startTimeStr     HH:mm:ss
			 * @param endTimeStr       HH:mm:ss
			 * @return
			 */
			public static boolean isUnusualDate(String nowTimeStr,String startTimeStr,String endTimeStr){
						Date nowTime = getDate(nowTimeStr,NOT_YMD_HH_MM_SS);
						Date startTime = getDate(startTimeStr,NOT_YMD_HH_MM_SS);
						Date endTime = getDate(endTimeStr,NOT_YMD_HH_MM_SS);
	       		return isEffectiveDate(nowTime,startTime,endTime);
		   }

			/**
			 * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
			 *
			 * @param nowTime 当前时间
			 * @param startTime 开始时间
			 * @param endTime 结束时间
			 * @return
			 * @author jqlin
			 */
			public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
				if (nowTime.getTime() == startTime.getTime()
						|| nowTime.getTime() == endTime.getTime()) {
					return true;
				}

				Calendar date = Calendar.getInstance();
				date.setTime(nowTime);

				Calendar begin = Calendar.getInstance();
				begin.setTime(startTime);

				Calendar end = Calendar.getInstance();
				end.setTime(endTime);

				if (date.after(begin) && date.before(end)) {
					return true;
				} else {
					return false;
				}
			}

	/**
	 * 自动识别转化时间
	 * @param timeStr
	 * @return
	 */
	public static Date parseDateForRobotization(String timeStr){

		return getDate(timeStr,FORMATSTR.get(REGEXS.get(getRegex(timeStr))));
	}

	/**
	 * 将传入的时间字符串摒弃年月日转化为时间
	 * @param timeStr
	 * @return
	 */
	public static Date parseDateForRobotizationHhmmss(String timeStr){
		return parseDateForRobotization(backFormatDate(parseDateForRobotization(timeStr),NOT_YMD_HH_MM_SS));
	}

	/**
	 * 由过去的某一时间,计算距离当前的时间
	 */
	public static String calculateTime(Date checkDate) {
		// 获取当前时间的毫秒数

		long nowTime = System.currentTimeMillis();

		String msg = null;
		// 获取指定时间的毫秒数
		long reset = checkDate.getTime();





		long dateDiff = nowTime - reset;
		if (dateDiff < 0) {
			msg = "输入的时间不对";
		} else {
			//判断年份是否相同 不相同直接返回时间字符串
			//不相同情况下超过》=2直接返回几年前字符串--否则返回年月日时分秒
			Date now = new Date(nowTime);
			if(!getYear(now).equals(getYear(checkDate))){
				//跨年直接显示字符串
				Integer year = Integer.parseInt(getYear(now))-Integer.parseInt(getYear(checkDate));
				if(year>=2){
					msg =  year+"年前";
				}else {
					msg =  backFormatDate(checkDate,YYYY_MM_DD_HH_MM_SS);
				}
			}else{
				//相同情况下判断 月 天 小时  分钟
				// 秒
				long dateTemp1 = dateDiff / 1000;
				// 分钟
				long dateTemp2 = dateTemp1 / 60;
				// 小时
				long dateTemp3 = dateTemp2 / 60;
				// 天数
				long dateTemp4 = dateTemp3 / 24;
				// 月数
				long dateTemp5 = dateTemp4 / 30;
				// 年数
				long dateTemp6 = dateTemp5 / 12;
			    if (dateTemp4 > 0) {
					if(dateTemp4>=3){
						msg =  backFormatDate(checkDate,YYYY_MM_DD_HH_MM_SS);
					}else{
						msg = dateTemp4 + "天前";
					}
				} else if (dateTemp3 > 0) {
					msg = dateTemp3 + "小时前";
				} else if (dateTemp2 > 0) {
					msg = dateTemp2 + "分钟前";
				} else if (dateTemp1 > 0) {
					msg = "刚刚";
				}
			}

		}
		return msg;
	}

 }
